package com.xjrh.traintravel.collector.netty.coder;

import com.xjrh.traintravel.collector.common.StringUtil;
import com.xjrh.traintravel.collector.netty.coder.command.AgreementStructure;
import lombok.extern.slf4j.Slf4j;

/**
 * 编码器
 *
 * @author snps
 * @create 2022-01-17 14:10
 * @describe
 **/
@Slf4j
public class Encoder {

    /**
     * 编码
     * @param agreement 协议对象
     * @return 16进制字符串
     */
    public static String encode(AgreementStructure agreement) {
        if (agreement == null) {
            log.error("服务端要发送的协议数据为空!");
            return null;
        }

        StringBuilder sbuAgreement = new StringBuilder();
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getPc())?agreement.getPc():StringUtil.STRING_EMPTY);
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getSd())?agreement.getSd():StringUtil.STRING_EMPTY);
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getFc())?agreement.getFc():StringUtil.STRING_EMPTY);
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getAddr())?agreement.getAddr():StringUtil.STRING_EMPTY);
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getSeq())?agreement.getSeq():StringUtil.STRING_EMPTY);
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getLen())?agreement.getLen():StringUtil.STRING_EMPTY);
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getData())?agreement.getData():StringUtil.STRING_EMPTY);
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getFcs())?agreement.getFcs():StringUtil.STRING_EMPTY);
        sbuAgreement.append(StringUtil.isNotEmpty(agreement.getFs())?agreement.getFs():StringUtil.STRING_EMPTY);

        return sbuAgreement.toString();
    }

}